๐Ÿ’ป Interceptor

@Seongukbaek Posted March 15, 2022 ยท Updated March 17, 2022 ยท 7 min read

GongDon ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด์„œ ์•Œ๊ฒŒ ๋œ Interceptor์˜ ๊ฐœ๋…์— ๋Œ€ํ•ด ์ •๋ฆฌํ•˜๋Š” POST๋‹ค.

๐Ÿ”ฆ Interceptor๋ž€

์ปจํŠธ๋กค๋Ÿฌ์— ๋“ค์–ด์˜ค๋Š” ์š”์ฒญ(HttpRequest)์™€ ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ์‘๋‹ตํ•˜๋Š” HttpResponse๋ฅผ ๊ฐ€๋กœ์ฑ„๋Š” ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ์ธ์ฆ ์ž‘์—…์ด๋‚˜ ํ•„์š”ํ•œ ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•œ ํ›„ ์ปจํŠธ๋กค๋Ÿฌ๋กœ ๋ณด๋‚ด์ฃผ๋Š” ๋ชจ๋“ˆ์ด๋‹ค.

์œ„ ๊ทธ๋ฆผ์„ ๋ณด๋ฉด, interceptor๋Š” DispatcherServlet ์˜ ์•ž, ๋’ค์—์„œ ์š”์ฒญ๊ณผ ์‘๋‹ต์„ ๊ฐ€๋กœ์ฑˆ๋‹ค. ์–ธ๋œป ๋ณด๋ฉด filter์™€ ๋น„์Šทํ•ด๋ณด์ด๋Š”๋ฐ, ๋ถ„๋ช…ํ•œ ์ฐจ์ด์ ์ด ์žˆ๋‹ค.

  1. ํ˜ธ์ถœ ์‹œ์ 

    • ๊ทธ๋ฆผ๋งŒ ๋ณด๋”๋ผ๋„, ํ™•์—ฐํžˆ ํ˜ธ์ถœ ์‹œ์ ์ด ๋‹ค๋ฅธ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
    • filter์˜ ๊ฒฝ์šฐ, DispatcherServlet ์ด ์‹คํ–‰๋˜๊ธฐ ์ „์— ํ˜ธ์ถœ๋œ๋‹ค.
    • interceptor์˜ ๊ฒฝ์šฐ, DispatcherServlet ์ด ์‹คํ–‰๋œ ์ดํ›„์— ํ˜ธ์ถœ๋œ๋‹ค.
  2. ์„ค์ • ์œ„์น˜

    • filter๋Š” web.xml ์— ์„ค์ •ํ•˜์ง€๋งŒ, interceptor๋Š” spring-boot-starter-web dependency ์„ค์ •๊ณผ interceptor ๋“ฑ๋ก์„ ์œ„ํ•œ config๊ฐ€ ํ•„์š”ํ•˜๋‹ค.
  3. ๊ตฌํ˜„ ๋ฐฉ์‹

    • filter๋Š” ์„ค์ • ์ดํ›„, ๋ฐ”๋กœ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
    • interceptor๋Š” ๋“ฑ๋ก๊ณผ ๋“ฑ๋กํ•œ ๋นˆ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์†Œ๋“œ ๊ตฌํ˜„์ด ํ•„์š”ํ•˜๋‹ค.

Interceptor์— ๋Œ€ํ•ด ๋” ์ž์„ธํ•˜๊ฒŒ ๋ณด๋ฉด, ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™๋‹ค.

  • ์œ„ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ Interceptor๋Š” Controller(Handler)๋กœ ๊ฐ€๊ธฐ์ „์— ์š”์ฒญ์„ ๊ฐ€๋กœ์ฑ„๋Š” preHandle , Controller ์ฒ˜๋ฆฌ ํ›„ postHandle , ์ „์ฒด ์š”์ฒญ์ด ๋๋‚œ ํ›„ ์ฒ˜๋ฆฌ๋˜๋Š” afterCompletion ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ์žˆ๋‹ค.
  • Handler ์—ญํ• ์„ ํ•˜๋Š” Controller๋กœ ๊ฐ€๊ธฐ์ „์— ๊ฐ€๋กœ์ฑ„๊ธฐ ๋•Œ๋ฌธ์— ์ •์‹ ๋ช…์นญ์€ HandlerInterceptor ์ด๋‹ค.

๐Ÿ”ฆ Interceptor ์„ค์ •

Interceptor ์‚ฌ์šฉ์„ ์œ„ํ•ด์„œ๋Š”,

  • spring-boot-starter-web dependency ์„ค์ •
  • Interceptor ์ƒ์„ฑ
  • Interceptor ๋“ฑ๋ก์„ ์œ„ํ•œ Config

๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

๐Ÿ”ง Interceptor ์ƒ์„ฑ

๋จผ์ € Interceptor์˜ ์ƒ์„ฑ์„ ์œ„ํ•ด์„œ๋Š”, ์•„๋ž˜ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

  • Spring Web์—์„œ ์ œ๊ณตํ•˜๋Š” HandlerInterceptor Interface๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ• (preHandle , postHandle , afterCompletion ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•„์š”)
  • ์ถ”์ƒ ํด๋ž˜์Šค์ธ HandlerInterceptorAdapter ๋ฅผ ์ƒ์†ํ•˜๋Š” ๋ฐฉ๋ฒ•

ํ›„์ž๋ฅผ ํƒํ•˜๊ฒŒ ๋˜๋ฉด, @Deprecated annotation์— ๋Œ€ํ•œ ๊ณ ๋ฏผ์ด ํ•„์š”ํ•˜๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ „์ž์— ๋Œ€ํ•ด์„œ ๋‹ค๋ฃฌ๋‹ค.

@Deprecated

  • ํด๋ž˜์Šค๋‚˜ ๋ฉ”์†Œ๋“œ ๋“ฑ์„ ๋” ์ด์ƒ ์‚ฌ์šฉ ๊ถŒ์žฅํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•˜๋Š” annotation
  • ์‚ฌ์šฉ์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ, ์ฐจํ›„์— ์—†์–ด์งˆ ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.

MyInterceptor.java

@Log4j2 @Component public class PostInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception { log.info("url : {}", request.getRequestURI()); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object obj, ModelAndView mav) throws Exception { log.info("response status: {}", response.getStatus()); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object obj, Exception e) throws Exception { } }
  • preHandle : ์‹ค์ œ Controller(Handler)๊ฐ€ ์‹คํ–‰๋˜๊ธฐ ์ „์— ์‹คํ–‰๋˜๋ฉฐ, boolean ํƒ€์ž…์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

    • true : ์š”์ฒญํ•œ handler๋ฅผ ์ฒ˜๋ฆฌ
    • false : ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์Œ
  • postHandle : handler๊ฐ€ ์‹คํ–‰๋œ ํ›„ ์‹คํ–‰
  • afterCompletion : ์ „์ฒด ์š”์ฒญ์ด ๋๋‚˜๊ณ  ๋‚œ ํ›„ ๋งˆ์ง€๋ง‰์— ์‹คํ–‰

ํ•„์š”ํ•œ ๊ธฐ๋Šฅ์— ๋”ฐ๋ผ ๊ฐ ๋ฉ”์†Œ๋“œ์— ๊ตฌํ˜„ํ•˜๋ฉด ๋œ๋‹ค.

๐Ÿ”ง Interceptor ๋“ฑ๋ก

์œ„์—์„œ ์ƒ์„ฑํ•œ Interceptor๋Š” ์•„์ง ์ •์ƒ์ ์œผ๋กœ ๋“ฑ๋ก๋˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์—, ํ•ด๋‹น ์ฝ”๋“œ๋Š” ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค. Spring MVC์— ๋“ฑ๋ก ์š”์ฒญ์„ ์ˆ˜ํ–‰ํ•˜๋Š” Config๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

WebConfig.java

@Configuration @RequiredArgsConstructor public class WebConfig implements WebMvcConfigurer { private final PostInterceptor postInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(postInterceptor) .addPathPatterns("/api/post/**"); } }
  • Interceptor ๋“ฑ๋ก์„ ์œ„ํ•œ ์„ค์ • ํŒŒ์ผ์ด๋‹ค.
  • @Component annotation์œผ๋กœ Bean์œผ๋กœ ๋“ฑ๋ก๋œ Interceptor๋ฅผ ๊ฐ€์ ธ์™€ registry์— ๋“ฑ๋กํ•œ๋‹ค.

    • addPathPatterns : interceptor๋ฅผ ์ ์šฉํ•  patterns๋ฅผ ๋ช…์‹œ
    • excludePathPatterns : ์ œ์™ธํ•  patterns๋ฅผ ๋ช…์‹œ
    • ๊ฐ Controller์˜ ๋ฉ”์†Œ๋“œ์— ๋Œ€ํ•ด annotation์„ ์ด์šฉํ•ด ๊ถŒํ•œ์„ ํ™•์ธํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ชจ๋“  patterns์— ๋Œ€ํ•ด ๊ฐœ๋ฐฉํ•ด๋‘˜ ์ˆ˜ ์žˆ๋‹ค.
    • ๋‚˜๋Š” ์—ฌ๊ธฐ์„œ PostController ์— ๋Œ€ํ•œ Interceptor๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด /api/post/** ๋กœ ๋ช…์‹œํ–ˆ๋‹ค.

annotation ๋ฐฉ์‹์„ ํ™œ์šฉํ•˜์—ฌ Interceptor์—์„œ ์ˆ˜ํ–‰ํ•  ๊ถŒํ•œ ํ™•์ธ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์•„๋ž˜๋Š” MyAnnotation.java

@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MyAnnotation { // ๊ถŒํ•œ์„ ํ™•์ธํ•  ๋กœ์ง, ์˜ˆ๋ฅผ ๋“ค๋ฉด ๊ด€๋ฆฌ์ž์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ํ™•์ธ UserType userType() default UserType.STUDENT; }
  • handlerMethod์— annotation์„ ํ™œ์šฉํ•œ ๊ถŒํ•œ ํ™•์ธ์„ ์œ„ํ•ด ์ถ”๊ฐ€ํ•œ annotation์ด๋‹ค.

Spring Security์— ๋น„์Šทํ•œ ๊ธฐ๋Šฅ์„ ํ•˜๋Š” @Secured annotation์ด ์žˆ๋‹ค.

์ตœ์ข…์ ์œผ๋กœ Controller์˜ Methods์— MyAnnotation annotation์„ ์ถ”๊ฐ€ํ•ด ์ ์ ˆํžˆ ์ˆ˜ํ–‰๋˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

๐Ÿ”ฆ AOP ์‚ฌ์šฉ

AOP์— ๋Œ€ํ•ด์„œ๋Š” ๋‹ค์Œ ํฌ์ŠคํŠธ์—์„œ ์ •๋ฆฌํ•˜๋„๋ก ํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์œ„์—์„œ ์ƒ์„ฑํ•œ annotation์— ๋Œ€ํ•œ ์‹คํ–‰ ์‹œ๊ฐ„ ์ธก์ •์„ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•ด๋ณด๋„๋ก ํ•œ๋‹ค.

๋จผ์ € AOP ์‚ฌ์šฉ์„ ์œ„ํ•ด ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

implementation 'org.springframework.boot:spring-boot-starter-aop' @Component @Aspect // Aspect ๋ฅผ ๊ผญ ๋ช…์‹œ! @Log4j2 class LogAspect { @Around("@annotation(MyAnnotation)") // ํ•ด๋‹น ์–ด๋…ธํ…Œ์ด์…˜์— ๋Œ€ํ•ด์„œ public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable { StopWatch stopWatch = new StopWatch(); stopWatch.start(); Object proceed = joinPoint.proceed(); stopWatch.stop(); log.info(stopWatch.prettyPrint()); return proceed; } }
  • MyAnnotation ์ด ๋ถ™๊ฒŒ ๋˜๋ฉด, ์‹คํ–‰ ์‹œ๊ฐ„์„ ์ธก์ •ํ•˜๊ณ  ์ด๋ฅผ ๋กœ๊ทธ์— ๋‚จ๊ธฐ๋„๋ก ํ•œ๋‹ค.

    • Aspect ๋กœ, ์ด ํด๋ž˜์Šค๊ฐ€ Aspect๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํด๋ž˜์Šค๋ผ๋Š” ๊ฒƒ์„ ๋ช…์‹œํ•œ๋‹ค.
    • joinPoint ๋ฅผ ๊ฐ€์ ธ์™€์„œ, ํ•ด๋‹น Point์—์„œ ๋™์ž‘๋˜๋Š” ์‹œ๊ฐ„์„ ์ธก์ •ํ•œ๋‹ค.

์‹คํ–‰ ์‹œ๊ฐ„์„ ์ธก์ •ํ•  Controller method์— MyAnnotation ์„ ๋ถ™์—ฌ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•œ๋‹ค.

๐Ÿ“• ์ฐธ๊ณ 

@Seongukbaek
๋ฐฐ์šฐ๊ณ  ์ •๋ฆฌํ•˜๊ณ  ๊ณต์œ ํ•˜๋Š” ๋Œ€ํ•™์ƒ ๋ฐฑ์„ฑ์šฑ์ž…๋‹ˆ๋‹ค ๐Ÿ˜€